<template>
  <div class="header">
    <p class="logo">logo</p>
    <ul class="menu">
      <li
        :class="[index == currentIndex ? 'current' : '']"
        v-for="(item, index) in menu"
        :key="index"
        @click="go(item.path)"
      >
        <Ellipsis :content="item.meta.title">
          <span>{{ item.meta.title }}</span>
        </Ellipsis>
      </li>
    </ul>
    <a-dropdown overlayClassName="globalHeader-menu" placement="bottom">
      <template #overlay>
        <a-menu><a-menu-item @click="handleLogout">退出</a-menu-item></a-menu>
      </template>
      <div class="userBox">
        <span>{{ userName }}</span>
      </div>
    </a-dropdown>
  </div>
</template>

<script setup name="globalHeader">
  import { ref, watch, computed } from 'vue';
  import router, { menu } from '@/router';
  import { useUserStore } from '@/store/user';
  import { LOGIN_PAGE_PATH } from '@/mutation-type';
  import Ellipsis from '@/components/Ellipsis/index.vue';
  const headerRef = ref(null);
  const userStore = useUserStore();
  const userName = computed(() => userStore.user?.userName);

  const currentIndex = ref(-1);
  const go = path => {
    router.push(path);
    light(path);
  };
  const light = path => {
    console.log(path);
    let index = menu.findIndex(item => {
      let a = item.path.split('/'),
        b = path.split('/');
      return a[1] === b[1];
    });
    currentIndex.value = index;
  };
  watch(
    () => router.currentRoute.value,
    route => {
      light(route.path);
    },
    { immediate: true }
  );

  const handleLogout = () => {
    userStore.logout().then(res => {
      router.replace(LOGIN_PAGE_PATH);
    });
  };
</script>

<style lang="less" scoped>
  .header {
    height: 50px;
    color: #ffff;
    font-weight: bold;
    background: #000;
    display: flex;
    padding: 0 10px;
    align-items: center;
    justify-content: space-between;
    .menu {
      display: flex;
      li {
        margin-right: 10px;
        cursor: pointer;
        &.current {
          color: red;
        }
      }
      span {
        display: block;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        width: 70px;
      }
    }
    .userBox {
      line-height: 50px;
      height: 50px;
    }
  }
</style>
